import React from 'react';
import { Modal, Form, Row, Col, Input } from 'antd';
import { FormInstance } from 'antd/es/form/Form';
import { FlagEnum, FormModalProps } from '@/types/global';
import { RoleItem } from '@/types/role';
import { formLayout, basicFormItemLangLayout, maxModalFormItemLayout } from '@/utils/commons';

type UpdateRoleProps<T> = FormModalProps<T> & {
  flag: FlagEnum;
}

const FormItem = Form.Item;
const TextArea = Input.TextArea;

const UpdateRoleModal: React.FC<UpdateRoleProps<RoleItem>> = (props) => {
  const { formData, colon, modalWidth, modalVisible, loading, flag } = props;
  const formItemLayout = basicFormItemLangLayout;
  const modalTitle = (flag === 'edit' ? '修改角色' : '查看角色信息');
  const disabled = (flag === 'edit' ? false : true);
  const formRef = React.createRef<FormInstance>();

  const onOk = () => {
    const { formData, flag } = props;
    if (flag === 'view') {
      const { onHandlerOK } = props;
      if (onHandlerOK) {
        onHandlerOK(formData);
      }
    } else {
      const { roleId } = formData;
      console.log(formRef);
      formRef.current!.validateFields().then(fieldsValue => {
        console.info(fieldsValue);
        const values = {
          ...fieldsValue,
          roleId
        };
        console.log('Received values of form: ', values);
        const { onHandlerOK } = props;
        if (onHandlerOK) {
          onHandlerOK({
            ...values
          });
        }
      }).catch((err) => console.info('表单校验不通过'));
    }
  };

  const onCancel = () => {
    // 先清除form表单
    formRef.current!.resetFields();
    const {onHandlerCancel} = props;
    if (onHandlerCancel) {
      onHandlerCancel();
    }
  }

  return (
    <Modal
      title={modalTitle}
      destroyOnClose
      width={modalWidth}
      open={modalVisible}
      confirmLoading={loading}
      onOk={onOk}
      onCancel={onCancel}>
      <Form layout={formLayout} ref={formRef}>
        <Row>
          <Col span={12}>
            <FormItem label="角色编码" name='roleCode' {...formItemLayout} colon={colon}
              rules={[
                { max: 10, message: '角色编码最多允许输入10个字符' },
                { required: true, message: '角色编码必输' },
                { pattern: new RegExp(/^[A-Za-z][A-Za-z0-9]{1,50}$/, "g"), message: '角色编码只能以字母开头，由A-Za-z0-9组成，最大长度为10' }
              ]}
              initialValue={formData.roleCode}
            >
              <Input disabled />
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={12}>
            <FormItem label="角色名称" name='roleName' {...formItemLayout} colon={colon}
              rules={[
                { max: 20, message: '角色名称最多允许输入20个字符' },
                { required: true, message: '角色名称必输' },
              ]}
              initialValue={formData.roleName}
            >
              <Input disabled={disabled} />
            </FormItem>
          </Col>
        </Row>
        <Row>
          <Col span={24}>
            <FormItem label="角色描述" name='roleDesc' {...maxModalFormItemLayout} colon={colon}
              rules={[
                { max: 30, message: '角色描述最多允许输入30个字符' },
                { required: true, message: '角色描述' }
              ]}
              initialValue={formData.roleDesc}
            >
              <TextArea disabled={disabled} maxLength={30} showCount />
            </FormItem>
          </Col>
        </Row>
      </Form>
    </Modal>
  );

}

export default UpdateRoleModal;